import com.huaweicloud.sdk.iot.device.client.requests.CommandRsp;
import com.huaweicloud.sdk.iot.device.service.AbstractService;
import com.huaweicloud.sdk.iot.device.service.DeviceCommand;
import com.huaweicloud.sdk.iot.device.service.Property;

import java.util.Map;
import java.util.Random;

public class Dpzt extends AbstractService {

    //按照设备模型定义属性，注意属性的name和类型需要和模型一致，writeable表示属性知否可写，name指定属性名
    @Property(name = "alarm", writeable = true)                  //可读true
    int smokeAlarm = 1;

    @Property(name = "smokeConcentration", writeable = false)    //不可读false
    float concentration = 0.1f;

    public float getYqnd() {
        yqnd = new Random().nextFloat()*100.1f;

        return yqnd;
    }

    @Property(name = "yqnd", writeable = false)    //不可读false
    float yqnd = 0.1f;

    public float getGzqd() {
        gzqd = new Random().nextFloat()*100.1f;
        return gzqd;
    }

    @Property(name = "gzqd", writeable = false)    //不可读false
    float gzqd = 0.1f;

    public float getTrsd() {
        trsd = new Random().nextFloat()*100.1f;
        return trsd;
    }

    @Property(name = "trsd", writeable = false)    //不可读false
    float trsd = 0.1f;

    public float getTrwd() {
        trwd = new Random().nextFloat()*100.1f;
        return trwd;
    }

    @Property(name = "trwd", writeable = false)    //不可读false
    float trwd = 0.1f;
    @Property(writeable = false)
    int humidity;

    @Property(writeable = false)
    float temperature;

    public float getEyhtnd() {
        eyhtnd = new Random().nextFloat()*100.1f;
        return eyhtnd;
    }

    @Property(name = "eyhtnd", writeable = false)    //不可读false
    float eyhtnd = 0.1f;

    //定义命令，注意接口入参和返回值类型是固定的不能修改，否则会出现运行时错误
    @DeviceCommand(name = "ringAlarm")
    public CommandRsp alarm(Map<String, Object> paras) {


        int duration = (int) paras.get("duration");
        System.out.println("ringAlarm  duration = " + duration);
        //将log换成System.out.println
        CommandRsp commandRsp=new CommandRsp(0);
        commandRsp.setResponseName("COMMAND_RESPONSE");
        //定制
        return commandRsp;
    }

    //setter和getter接口的命名应该符合java bean规范，sdk会自动调用这些接口
    public int getHumidity() {

        //模拟从传感器读取数据
        humidity = new Random().nextInt(10);
        return humidity;
    }

    public void setHumidity(int humidity) {
        //humidity是只读的，不需要实现
    }

    public float getTemperature() {

        //模拟从传感器读取数据
        temperature = new Random().nextInt(10);
        return temperature;
    }

    public void setTemperature(float temperature) {
        //只读字段不需要实现set接口
    }

    public float getConcentration() {

        //模拟从传感器读取数据
        concentration = new Random().nextFloat()*10.2f;
        return concentration;
    }

    public void setConcentration(float concentration) {
        //只读字段不需要实现set接口
    }

    public int getSmokeAlarm() {
        smokeAlarm = new Random().nextInt(2);
        return smokeAlarm;
    }

    public void setSmokeAlarm(int smokeAlarm) {

        this.smokeAlarm = smokeAlarm;
        if (smokeAlarm == 0){
            System.out.println("alarm is cleared by app");
        }
    }


}
